//
//  PTHomeViewController.swift
//  PropertyClient
//
//  Created by benwang on 2021/6/22.
//

import UIKit
import CollectionKit

public class PTHomeViewController: UIViewController {

  var collectionView = CollectionView()
  let locationButtonItem = LocationButtonItem(maxLength: 12)
  let sheetHandler = PTSheetHandle()

  public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    opacityNavigationBar(color: .designKit.background)
  }

  public override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

      /**
       1.获取设备列表
       2.同步设备列表
       3.遍历列表获取



       */

      
      navigationItem.leftBarButtonItem = locationButtonItem
      locationButtonItem.text = "正在定位中..."

      view.backgroundColor = .designKit.secondaryBackground
      view.addSubview(collectionView)

      collectionView.snp.makeConstraints { maker in
        maker.edges.equalToSuperview()
      }

      let view = UIView()
      view.backgroundColor = .designKit.primary
      view.cornerRadius = 8

      let headerProvider = SimpleViewProvider(
          views: [view],
          sizeStrategy: (.fill, .absolute(140))
      )

      let headerTitles = ["公告通知", "新闻动态"]

      let dataSource0 = ArrayDataSource<HomeItem>(
        data: [HomeItem(text: "")]
      )

      let viewSource0 = ClosureViewSource<HomeItem, HomeTextContentView> { item, index in
        return R.nib.homeTextContentView(owner: self)!
      } viewUpdater: { item, data, index in

      }
      let sizeSource0 = { (index: Int, data: HomeItem, size: CGSize) in
        return CGSize(width: size.width, height: 60)
      }
      let itemProvider0 = BasicProvider<HomeItem, HomeTextContentView>(
        dataSource: dataSource0,
        viewSource: viewSource0,
        sizeSource: sizeSource0,
        layout: FlowLayout(spacing: 13)
      )

      itemProvider0.animator = FadeAnimator()
      itemProvider0.tapHandler = { (context) in
        self.jumpToNotice()
      }

      let dataSource1 = ArrayDataSource<HomeItem>(
        data: HomeItem.items
      )

      let viewSource1 = ClosureViewSource<HomeItem, HomeItemContentView> { item, index in
        return R.nib.homeItemContentView(owner: self)!
      } viewUpdater: { item, data, index in

      }
      let sizeSource1 = { (index: Int, data: HomeItem, size: CGSize) in
        return CGSize(width: size.width, height: 80)
      }
      let itemProvider1 = BasicProvider<HomeItem, HomeItemContentView>(
        dataSource: dataSource1,
        viewSource: viewSource1,
        sizeSource: sizeSource1,
        layout: FlowLayout(spacing: 15)
      )

      itemProvider1.animator = FadeAnimator()
      itemProvider1.tapHandler = { (context) in
        self.jumpToWeb()
      }

      let headL = ClosureViewSource<HeaderData, HomeHeaderSection> { item, index in
        return R.nib.homeHeaderSection(owner: self)!
      } viewUpdater: { item, data, index in
        item.unreadButton.isHidden = index != 0
        item.tagImageView.isHidden = index != 0
        item.unreadButton.isHidden = index != 0
        item.titleLabel.text = headerTitles[index]
      }

      let provider = ComposedHeaderProvider(
        layout: FlowLayout(spacing: 15),
        headerViewSource: headL,
        headerSizeSource: { (index, data, maxSize) -> CGSize in
          return CGSize(width: maxSize.width, height: .twoExtraLarge)
        },
        sections: [itemProvider0, itemProvider1]
      )

      collectionView.provider = ComposedProvider(
        layout: FlowLayout(spacing: 15).inset(by: .padding(.horizontal, 15)),
        sections: [headerProvider, provider]
      )
    }

  func jumpToNotice() {
    let vc = NoticeBilboardViewController()
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }

  func jumpToWeb() {
    let vc = WebViewController()
    vc.title = "新闻详情"
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


struct HomeItem {
  let text: String
}
extension HomeItem {
  static let items: [HomeItem] = [
    .init(text: ""),
    .init(text: ""),
    .init(text: ""),
    .init(text: ""),
    .init(text: ""),
  ]
}
